package huaweiod.first;

import java.util.*;

/**
 * 华为OD机试真题 Java 实现【最长的密码】【2022.11 Q4 新题】
 */
public class A_2023_25_MaxPassword {

    public static String maxPassword(List<String> list) {
        Set<String> set = new HashSet<>(list);
        int max = 0;
        String ans = "";
        for (String s : list) {
            boolean flag = true;
            for (int i = 1; i < s.length(); i++) {
                if(!set.contains(s.substring(0,i))) {
                    flag = false;
                    break;
                }
            }
            if(flag) {
                if(s.length() > max) {
                    max = s.length();
                    ans = s;
                }
                if(s.length() == max) {
                    ans = s.compareTo(ans) >= 1 ? s : ans;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        // 处理输入
//        String str = "h he hel hell hello";     // hello
        String str = "b ereddred bw bww bwwl bwwlm bwwln";
        List<String> list = Arrays.asList(str.split(" "));      // bwwln
        System.out.println(maxPassword(list));
    }
}
